package com.cms.controller.system;

import java.security.NoSuchAlgorithmException;

import javax.crypto.NoSuchPaddingException;

import com.cms.controller.BaseController;
import com.cms.model.bo.BussinessMsg;
import com.cms.model.dto.PwdDto;
import com.cms.model.vo.SysUser;
import com.cms.service.SysRoleService;
import com.cms.service.SysUserService;
import com.cms.utils.annotation.SystemControllerLog;
import com.cms.utils.constant.BussinessCode;
import com.cms.utils.tools.BussinessMsgUtil;
import com.cms.utils.tools.MD5Utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("user")
public class SysUserController extends BaseController {
	
    @Autowired
    private SysUserService userService;
    
    @Autowired
    private SysRoleService sysRoleService;

    /**
     *跳转到用户列表页面
     * @return
     */
    @RequestMapping("/user_list.do")
    public String toUserListPage() {
        return "system/user_list";
    }
    /**
     * 加载用户列表List
     * @param user
     * @return
     */
    @RequestMapping("/ajax_user_list.do")
    @ResponseBody
    public String ajaxUserList(SysUser user){
        return userService.selectUserResultPageList(user);
    }

    /**
     * 跳转到用户新增页面
     * @return
     */
    @RequestMapping("/user_add.do")
    public String toUserAddPage(Model model) {
        //新增页面标识
        model.addAttribute("pageFlag", "addPage");
        return "system/user_edit";
    }

    /**
     * 跳转到用户修改页面
     * @param userId 用户Id
     * @return
     */
    @RequestMapping("/user_update.do")
    public String userUpdatePage(Model model,Integer userId){
        SysUser user = userService.selectByPrimaryKey(userId);
        //修改页面标识
        model.addAttribute("pageFlag", "updatePage");
        model.addAttribute("user", user);
        return "system/user_edit";
    }
    
    /**
     * 保存用户信息
     * @param user 用户实体
     * @return
     */
    @RequestMapping("/ajax_save_user.do")
    @ResponseBody
    @SystemControllerLog(description="保存用户信息")
    public BussinessMsg ajaxSaveUser(SysUser user){
        try {
            return userService.saveOrUpdateUser(user, this.getCurrentLoginName());
        } catch (Exception e) {
            log.error("保存用户信息方法内部错误",e);
            return BussinessMsgUtil.returnCodeMessage(BussinessCode.USER_SAVE_ERROR);
        }
    }

    /**
     * 批量失效用户
     * @param userIds 用户Id
     * @return
     */
    @RequestMapping("/ajax_user_batch_fail.do")
    @ResponseBody
    @SystemControllerLog(description="批量失效用户信息")
    public BussinessMsg ajaxUserBatchFail(@RequestParam(value = "userIds[]") String[] userIds){
        try {
            return userService.updateUserBatchStatus(userIds, this.getCurrentLoginName());
        } catch (Exception e) {
            log.error("批量失效用户方法内部错误",e);
            return BussinessMsgUtil.returnCodeMessage(BussinessCode.USER_FAIL_ERROR);
        }
    }

    /**
     * 跳转到用户角色分配页面
     * @param userId 用户Id
     * @return
     */
    @RequestMapping("/user_grant.do")
    public String userGrantPage(Model model,Integer userId){
        SysUser user = userService.selectUserRolesByUserId(userId);
        model.addAttribute("user", user);
        return "system/user_grant";
    }

    /**
     * 查询待分配的角色信息(用以给用户分配角色时显示)
     * @param  roleIds 已分配角色Id
     */
    @RequestMapping("/ajax_undistributed_role_list.do")
    @ResponseBody
    public String ajaxUndistributedRoleList(String roleIds){
        return sysRoleService.selectUserRoleByRoleIdList(roleIds);
    }
    /**
     * 查询状态为有效,已分配的角色信息(用已用户分配角色显示)
     * @param roleIds 已分配角色Id
     */
    @RequestMapping("/ajax_deceased_role_list.do")
    @ResponseBody
    public String ajaxDeceasedRoleList(String roleIds){
        return sysRoleService.selectDeceasedUserRoleByRoleIdList(roleIds);
    }

    /**
     * 保存用户分配角色信息
     * @param userId 用户Id
     * @param roleIds 分配的角色信息
     * @return
     */
    @RequestMapping("/ajax_save_user_role.do")
    @ResponseBody
    @SystemControllerLog(description="用户分配角色")
    public BussinessMsg ajaxSaveUserRole(Integer userId,String roleIds){
        try {
           return  userService.saveUserRole(userId,roleIds, this.getCurrentLoginName());
        } catch (Exception e) {
            log.error("用户分配角色信息方法内部错误",e);
            return BussinessMsgUtil.returnCodeMessage(BussinessCode.USER_ROLE_SAVE_ERROR);
        }
   }
    
    /**
     * 跳转到修改密码页面
     * @return
     * @throws NoSuchPaddingException 
     * @throws NoSuchAlgorithmException 
     */
    @RequestMapping("/edit_pwd_page.do")
    public String toEditPwdPage(Model model) throws Exception {
        SysUser user = userService.selectByPrimaryKey(this.getCurrentLoginId());
        model.addAttribute("user", user);
        return "system/pwd_edit";
    }
    
    /**
     * 修改密码
     * @return
     */
    @RequestMapping("/edit_pwd.do")
    @ResponseBody
    public BussinessMsg updatePwd(PwdDto dto){
        try {
        	return userService.updatePassword(dto);
        } catch (Exception e) {
             log.error("修改密码方法内部错误",e);
             return BussinessMsgUtil.returnCodeMessage(BussinessCode.USER_ROLE_SAVE_ERROR);
        }
    }
    
    /**
     * 解锁密码
     * @return
     */
    @RequestMapping("/unlock_pwd.do")
    @ResponseBody
    public BussinessMsg unlockPwd(String unlockPwd){
        try {
        	SysUser user = userService.selectByPrimaryKey(this.getCurrentLoginId());
            if(!MD5Utils.encrytor(unlockPwd).equals(user.getUserPassword())){
            	return BussinessMsgUtil.returnCodeMessage(BussinessCode.GLOBAL_PASS_FAIL);
            }
        } catch (Exception e) {
             log.error("解锁密码方法内部错误",e);
             return BussinessMsgUtil.returnCodeMessage(BussinessCode.GLOBAL_PASS_FAIL);
        }
        return BussinessMsgUtil.returnCodeMessage(BussinessCode.GLOBAL_SUCCESS);
    }
}
